﻿using TrueSync;

namespace HEFramework
{
    public static class MonsterAIComponentSystem
    {
        [EntitySystem]
        public class MonsterAIComponentAwakeSystem : AwakeSystem<MonsterAIComponent>
        {
            protected override void Awake(MonsterAIComponent _self)
            {
            }
        }


        [EntitySystem]
        public class MonsterAIComponentUpdateSystem : UpdateSystem<MonsterAIComponent>
        {
            protected override void Update(MonsterAIComponent _self)
            {
                var ac = _self.GetParentChild<AttributeComponent>();
                var atc = _self.GetParentChild<ATKComponent>();
                var mc = _self.GetParentChild<MoveComponent>();
                var alc = _self.GetParentChild<AlertComponent>();
                var tc = _self.GetParentChild<TransformComponent>();
                if (ac == null || atc == null || mc == null || alc == null || tc == null)
                {
                    return;
                }

                //在攻击范围内就不移动了
                if (atc.IsTargetInRange())
                {
                    mc.SetTarget(tc.P, tc.P, 0);
                }
                else
                {
                    var entity = alc.Target.Entity;
                    if (entity != null)
                    {
                        if (atc.State == eATKState.Wait)
                        {
                            var o_tc = entity.GetChild<TransformComponent>();
                            mc.SetTarget(tc.P, o_tc.P, ac.MV_SP.Value);
                        }
                    }
                    else
                    {
                        mc.SetTarget(tc.P, new TSVector(0, 0, 0), ac.MV_SP.Value);
                    }
                }
            }
        }
    }
}